<?php
namespace Index\Controller;
use Index\Controller\CommonController;
use Common\Common\Tree;
class PublishController extends CommonController {
	/**
	 * 发布问题方法
	 */
    public function indexAction(){
		//获得分类
		$model = M();
		$sql = "select * from category";
		$cateData = $model->query($sql);


		$tree = new Tree();
		$resdata = $tree->categorytree($cateData);

		$this->assign('cateData',$resdata);

		//发布问题
    	if (IS_POST) {
    		$title = I('post.question_content');
    		$cid = I('post.category');
    		$content = I('post.content');
    		$uid = $_SESSION['uid'];
    		$create_time = time();
    		// var_dump($title,$cid,$content,$uid);

    		$Model = M();
    		$sql = "insert into `question` (title,cid,content,uid,create_time) VALUES('{$title}','{$cid}','{$content}','{$uid}','{$create_time}')";
    		$id = $Model->execute($sql);
    		if ($id) {
    			$this->success('发表成功',U("Index/publish/Index"),2);
    		}
    	}else{
    		$this->display('index');	
    	}
        
    }

	/**
	 * 百度编辑器
	 */
    public function ueditorAction(){
    	$data = new \Org\Util\Ueditor();
    	echo $data->output();
    }


    /**
     * 修改问题页面
     */
    public function editorAction(){

		//获得分类
		$model = M();
		$sql = "select * from category";
		$cateData = $model->query($sql);
		$tree = new Tree();
		$resdata = $tree->categorytree($cateData);
		$this->assign('cateData',$resdata);


		//获得老数据
		$id = I('get.id');
//		var_dump($id);
		$model = M();
		$sql = "select * from question WHERE id='{$id}'";
		$question = $model->query($sql);
		$question = $question[0];
//		var_dump($question);
		$this->assign('question',$question);
        $this->display('editor');
    }


//修改问题方法
	public function addAction(){
		$id = I('get.id');
		$title = I('post.question_content');
		$cid = I('post.category');
		$content = I('post.content');
		$create_time = time();
//		var_dump($title,$cid,$content,$id);
		$model = M();
		$sql = "update question set title='{$title}',content='{$content}',cid='{$cid}',update_time='{$create_time}' where id='{$id}'";
		$model->execute($sql);
		$this->success('修改成功',U("Admin/Problem/index"),2);
	}

}